package com.psjj.privilege.service;

import com.psjj.privilege.dao.RoleDao;
import com.psjj.privilege.dao.UserDao;
import com.psjj.privilege.po.Role;
import com.psjj.privilege.po.User;
import com.psjj.privilege.utils.MD5Util;

import java.sql.SQLException;
import java.util.List;

public class UserService {
    private UserDao userDao = new UserDao();
    private RoleDao roleDao=new RoleDao();



    public User loginJoinRoles(String username, String pwd) {
        pwd=MD5Util.getMD5Str(pwd);
        User user=null;
        try {
            user=userDao.queryUserByNameAndPwd(username,pwd);
            if(user!=null){
                int uid = user.getUid();
                List<Role> roles = roleDao.queryRolesByUid(uid);
                user.setRoles(roles);



            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;


    }

    public int getCount(String username) {
        int count = 0;
        try {
            count = userDao.getCount(username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    public List<User> getPage(String username, int begin, int pageSize) {
        List<User> list = null;
        try {
            list = userDao.getPage(username,begin,pageSize);
            list.forEach(user -> {
               int uid = user.getUid();
                List<Role> roles = null;
                try {
                    roles = roleDao.queryRolesByUid(uid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                user.setRoles(roles);




            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public void addUserJoinRoles(User user, String[] rids) {
        try {
            user.setPwd(MD5Util.getMD5Str(user.getPwd()));
            int uid = userDao.add(user);
            if(rids!=null && rids.length>0){
                for(String rid:rids){
                    userDao.addUidAndRid(uid,Integer.parseInt(rid));

                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public User queryUserByUid(int uid) {
        User user = null;
        try {
            user = userDao.queryUserByUid(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    public void editUserJoinRole(User user, String[] rids) {
        try {
            userDao.editUser(user);
            userDao.clearUserIdJoinRoleIdByUserId(user.getUid());
            userJoinRoleByUidAndRid(user,rids);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void userJoinRoleByUidAndRid(User user, String[] rids) {
        try {
            if(rids!=null && rids.length>0){
                for(String rid:rids){
                    userDao.addUidAndRid(user.getUid(),Integer.parseInt(rid));

                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public void del(int uid) {
        try {
            User user = userDao.queryUserByUid(uid);
            int status=0;
            if(user.getStatus()==1){
                status=0;

            }else{
                status=1;
            }
            userDao.updateUserStatusByUid(uid,status);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void resetPwd(int uid) {
        try {
            userDao.updatePwdByUid(uid,MD5Util.getMD5Str("123456"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void updatePassword(String username, String newPwd) {
        try {
            userDao.updatePwdByUsername(username,MD5Util.getMD5Str(newPwd));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
